﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Utilities
{
    public class CommonHelper
    {
        public static void Print(string str)
        {
            System.Diagnostics.Trace.Write(str);
        }
        public static void Println(string str)
        {
            System.Diagnostics.Trace.WriteLine(str);
        }
        public static void Println()
        {
            System.Diagnostics.Trace.WriteLine("                          ");
        }

        /// <summary>
        /// 是否数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsArray(dynamic value)
        {
            if (typeof(Array).IsInstanceOfType(value) || typeof(JArray).IsInstanceOfType(value)) return true;
            return false;
        }
        /// <summary>
        /// 是否J数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsJArray(dynamic value)
        {
            if (typeof(JArray).IsInstanceOfType(value)) return true;
            return false;
        }
        /// <summary>
        /// 是否列表
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsList(dynamic value)
        {
            try
            {
                value.ToObject<List<dynamic>>();
            }
            catch (Exception)
            {

                return false;
            }
            //if (value is IList) return true;
            return true;
        }
        /// <summary>
        /// 是否数组 或 列表
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsArrayOrList(dynamic value)
        {
            if (IsArray(value) || IsList(value)) return true;
            return false;
        }
        /// <summary>
        /// 是否字典
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDictionary(dynamic value)
        {
            if (typeof(IDictionary).IsInstanceOfType(value)) return true;
            return false;
        }
        /// <summary>
        /// 是否常规数据
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool isGeneral(dynamic value)
        {

            if (TypeHelper.IsNumeric(value.ToString()) || typeof(string).IsInstanceOfType(value))
            {
                return true;
            }
            return false;
        }

        public static string ToString(Dictionary<string, dynamic> data)
        {
            string str = JsonHelper.SerializeObject(data);
            return str.Replace("\\", "");
        }

        public static string Join(List<int> list, string str)
        {
            StringBuilder builder = new StringBuilder();
            int size = list.Count;
            for (int i = 0; i < size; i++)
            {
                dynamic item = list[i];
                builder.Append(item.ToString());
                if (i < size - 1) builder.Append(str);
            }
            return builder.ToString();
        }

        public static string Join(List<string> list, string str)
        {
            StringBuilder builder = new StringBuilder();
            int size = list.Count;
            for (int i = 0; i < size; i++)
            {
                dynamic item = list[i];
                builder.Append("'");
                builder.Append(item.ToString());
                builder.Append("'");
                if (i < size - 1) builder.Append(str);
            }
            return builder.ToString();
        }

        public static Dictionary<string, dynamic> ToDictionary(dynamic data)
        {
            Dictionary<string, dynamic> dic = new Dictionary<string, dynamic>();
            foreach (var item in data)
            {
                string key = item.Key;
                dynamic value = item.Value;
            }
            return dic;
        }

        public static List<Dictionary<string, dynamic>> ToDictionary(List<dynamic> datas)
        {
            List<Dictionary<string, dynamic>> dics = new List<Dictionary<string, dynamic>>();
            foreach (var item in datas)
            {
                Dictionary<string, dynamic> dic = ToDictionary(item);
                dics.Add(dic);
            }
            return dics;
        }

        public static void ListAdd(ref List<int> list, List<int> adds)
        {
            for (int j = 0; j < adds.Count; j++)
            {
                int id = adds[j];
                if (!list.Contains(id)) list.Add(id);
            }
        }
        /// <summary>
        /// 数据对象列表转字段列表
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static List<Dictionary<string, dynamic>> toDics(List<dynamic> datas)
        {
            List<Dictionary<string, dynamic>> list = new List<Dictionary<string, dynamic>>();
            foreach (IEnumerable item in datas)
            {
                list.Add(toDic(item));
            }
            return list;
        }
        /// <summary>
        /// 单个数据对象转字段
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Dictionary<string, dynamic> toDic(IEnumerable data)
        {
            Dictionary<string, dynamic> dic = new Dictionary<string, dynamic>();
            foreach (KeyValuePair<string, object> item in data)
            {
                string key = item.Key;
                object value = item.Value;
                dic.Add(key, value);
            }
            return dic;
        }
        /// <summary>
        /// 获取指定key的值
        /// </summary>
        /// <param name="data"></param>
        /// <param name="assignKey"></param>
        /// <returns></returns>
        public static dynamic getValue(IEnumerable data, string assignKey)
        {
            foreach (KeyValuePair<string, object> item in data)
            {
                string key = item.Key;
                if (assignKey.Equals(key))
                {
                    return item.Value;
                }
            }
            return null;
        }
        /// <summary>
        /// 获取指定key的值
        /// </summary>
        /// <param name="data"></param>
        /// <param name="assignKey"></param>
        /// <returns></returns>
        public static dynamic getValueByDic<T>(Dictionary<string, T> data, string assignKey)
        {
            foreach (var item in data)
            {
                string key = item.Key;
                if (assignKey.Equals(key))
                {
                    return item.Value;
                }
            }
            return null;
        }

        public static Dictionary<string, dynamic> buildPaging<T>(List<T> list, int currpage, int total, string listKey = "list")
        {
            Dictionary<string, dynamic> data = new Dictionary<string, dynamic>();
            data.Add(listKey, list);
            data.Add("currpage", currpage);
            data.Add("total", total);
            return data;
        }

        public static Dictionary<string, dynamic> buildPaging<T>(List<T> list, Paging paging, string listKey = "list")
        {
            Dictionary<string, dynamic> data = new Dictionary<string, dynamic>();
            data.Add(listKey, list);
            data.Add("currpage", paging.currentPage);
            data.Add("total", paging.total);
            return data;
        }

        /// <summary>
        /// 是否为中文
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsContainChinese(string input)
        {
            string pattern = "[\u4e00-\u9fbb]";
            return Regex.IsMatch(input, pattern);
        }
        /// <summary>
        /// 是否为数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumeric(string value)
        {
            Regex reg = new Regex("^[0-9]+$");
            Match ma = reg.Match(value);
            if (ma.Success)
            {
                //是数字
                return true;
            }
            return false;
        }

        /// <summary>
        /// 根据key从对象中获取值，若不存在则返回null
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static dynamic GetValue(dynamic obj, string key)
        {
            dynamic value = DynamicHelper.IsPropertyExist(obj, key) ? obj[key] : null;
            return value;
        }

        public static int FormatInt(dynamic val, int defaultValue = 0)
        {
            string str = val != null ? $"{val}" : "";
            return string.IsNullOrEmpty(str) ? defaultValue : int.Parse(str);
        }
        public static long FormatLong(dynamic val, long defaultValue = 0)
        {
            string str = val != null ? $"{val}" : "";
            return string.IsNullOrEmpty(str) ? defaultValue : long.Parse(str);
        }
        public static decimal FormatDecimal(dynamic val, decimal defaultValue = 0)
        {
            string str = val != null ? $"{val}" : "";
            return string.IsNullOrEmpty(str) ? defaultValue : decimal.Parse(str);
        }

        public static double FormatDouble(dynamic val, double defaultValue = 0)
        {
            string str = val != null ? $"{val}" : "";
            return string.IsNullOrEmpty(str) ? defaultValue : double.Parse(str);
        }

        /// <summary>
        /// 根据key从对象中获取值，若不存在则返回默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetInt(dynamic obj, string key, int defaultValue = 0)
        {
            string str = GetString(obj, key);
            return FormatInt(str, defaultValue);
        }

        /// <summary>
        /// 根据key从对象中获取值，若不存在则返回默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static long GetLong(dynamic obj, string key, long defaultValue = 0)
        {
            string str = GetString(obj, key);
            return FormatLong(str, defaultValue);
        }

        /// <summary>
        /// 根据key从对象中获取值，若不存在则返回默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static decimal GetDecimal(dynamic obj, string key, decimal defaultValue = 0)
        {
            string str = GetString(obj, key, $"{defaultValue}");
            return FormatDecimal(str, defaultValue);
        }

        /// <summary>
        /// 根据key从对象中获取值，若不存在则返回默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static double GetDouble(dynamic obj, string key, double defaultValue = 0)
        {
            string str = GetString(obj, key, $"{defaultValue}");
            return FormatDouble(str, defaultValue);
        }

        /// <summary>
        /// 根据key从对象中获取值，若不存在则返回默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetString(dynamic obj, string key, string defaultValue = "")
        {
            if (obj == null) return defaultValue;
            dynamic value = DynamicHelper.IsPropertyExist(obj, key) ? obj[key] : null;
            string str = value != null ? $"{value}" : defaultValue;
            return str;
        }

        public static string GetString(dynamic value)
        {
            try
            {
                return value != null ? (value is object ? value : "") : "";
            }
            catch (Exception)
            {

                return "";
            }
        }
    }
}
